/**
 * 
 */
package com.flying.dcss.userpermissions.action;

import gnu.io.CommPortIdentifier;

import java.io.PrintWriter;

import java.util.Date;
import java.util.Enumeration;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.flying.dcss.app.CommonHelper;
import com.flying.dcss.app.MD5Helper;
import com.flying.dcss.app.SessionName;
import com.flying.dcss.common.service.impl.MailService;
import com.flying.dcss.common.service.impl.SmsService;
import com.flying.dcss.userpermissions.bo.impl.DcssUser;
import com.flying.dcss.userpermissions.bo.impl.DcssUserInfo;
import com.flying.dcss.userpermissions.service.impl.UserInfoService;
import com.flying.dcss.userpermissions.service.impl.UserService;
import com.opensymphony.xwork2.ActionSupport;

// TODO: Auto-generated Javadoc
/**
 * The Class RegisterAction.
 * 
 * @author EvanHong
 */
public class RegisterAction extends ActionSupport {

	/** The mail service. */
	private MailService mailService;

	/** The sms service. */
	private SmsService smsService;

	/** The user info service. */
	private UserInfoService userInfoService;

	/**
	 * Sets the user info service.
	 *
	 * @param userInfoService the new user info service
	 */
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	/**
	 * Sets the mail service.
	 * 
	 * @param mailService
	 *            the new mail service
	 */
	public void setMailService(MailService mailService) {
		this.mailService = mailService;
	}

	/**
	 * Sets the sms service.
	 *
	 * @param smsService the new sms service
	 */
	public void setSmsService(SmsService smsService) {
		this.smsService = smsService;
	}

	/** The user. */
	private DcssUser user;

	/** The user service. */
	private UserService userService;

	/**
	 * Sets the user.
	 * 
	 * @param user
	 *            the new user
	 */
	public void setUser(DcssUser user) {
		this.user = user;
	}

	/**
	 * Gets the user.
	 * 
	 * @return the user
	 */
	public DcssUser getUser() {
		return user;
	}

	/**
	 * Sets the user service.
	 * 
	 * @param userService
	 *            the new user service
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() throws Exception {

		// userService.userRegister(user);

		// If email exist.
		if (!userService.isValidEmail(user.getEmail())) {
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			out
					.print("<script>alert('E-mail already exists!');self.location='register.jsp';</script>");
			out.flush();
			out.close();
			return this.ERROR;
		}
		// Get User Ip.
		user.setRegisterIp(ServletActionContext.getRequest().getRemoteAddr());

		// Set password empty.
		DcssUser realUser = userService.userRegister(user);

		// Create User Info.
		DcssUserInfo realUserInfo = new DcssUserInfo();
		realUserInfo.setId(UUID.randomUUID().toString());
		realUserInfo.setuserId(realUser.getId());
		realUserInfo.setPhoneService(true);
		userInfoService.userInfoAdd(realUserInfo);

		// Verify process.
		String codeString = CommonHelper.getShortDate(realUser.getCreateTime())
				+ CommonHelper.getShortDate(new Date()) + user.getEmail();

		String verifyCode = MD5Helper.MD5Encode(codeString);

		mailService.sendVerificationEmail(user.getEmail(), verifyCode);

		// smsService.sendTestSMS("+8615072311876", "Hello GSM World!");

		return this.SUCCESS;
	}
}
